#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy
import actionlib
from ros1_protobuf_msg_bridge.action import ActDemoAction
from ros1_protobuf_msg_bridge.action import ActDemoGoal

def done_cb(feedback, status):
    rospy.loginfo("done_cb")
def active_cb():
    rospy.loginfo("active_cb")
def feedback_cb(feedback):
    #rospy.loginfo("Feedback")
    rospy.loginfo("Feedback:%f", feedback.pb.val)

def main():
    rospy.init_node('py_act_client')

    # 创建一个客户端
    client = actionlib.SimpleActionClient('t_act1', ActDemoAction)
    print("Waiting for server...")
    client.wait_for_server()
    print("Action server is up, we can send new goals!")

    # 创建一个请求对象
    goal = ActDemoGoal()
    goal.pb.key = "example"
    goal.pb.val = 123.45

    # 发送请求并监听
    client.send_goal(goal, done_cb,active_cb,feedback_cb)
    print("Goal has been sent")

    # 等待结果
    client.wait_for_result( rospy.Duration(30.0))
    print("Waiting for server result...")

    # 取出结果
    result = client.get_result() 
    # 检查action的状态
    if client.get_state() == actionlib.GoalStatus.SUCCEEDED:
        rospy.loginfo("Action SUCCEEDED")
        rospy.loginfo("client.get_result():%f", client.get_result().pb.val)
        # 这里可以处理成功的结果
    else:
        rospy.logerr("Action failed with status: %s", client.get_state())
        # 这里可以处理失败的情况，例如重试或记录错误

if __name__ == '__main__':
    main()
